from bs4 import BeautifulSoup
import json
# 假设html_content是包含上述HTML的字符串
with open('LOL_ARAM.html', 'r', encoding='utf-8') as file:
    html_content = file.read()
soup = BeautifulSoup(html_content, 'lxml')

# 创建BeautifulSoup对象
soup = BeautifulSoup(html_content, 'html.parser')
#使用列表嵌套字典最后转为json格式
champions_list = []
# 遍历tr元素（一个tr元素代表一个英雄）
trs = soup.findAll('tr')[2:-7]
for tr in trs :
    tds = tr.find_all("td")
    champions_name = tds[0].get_text(strip=True).split("-")[0]
    champions_debuff = ''
    champions_dict = {}
    champions_dict['name'] = champions_name
    #遍历td元素,英雄对应的debuff
    for i,td in enumerate(tds[1:-3]):
        champions_debuff = champions_debuff + td.get_text(strip=True)
    champions_dict['debuff'] = champions_debuff
    champions_list.append(champions_dict)
with open('champions_debuff.json', 'w', encoding='utf-8') as f:
    json.dump(champions_list, f, ensure_ascii=False, indent=4)

